package fan.codeGenerate.generate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fan.codeGenerate.common.Common;
import fan.codeGenerate.common.GenerateInfo;
import fan.codeGenerate.freeMarker.FreeMarker;

/** 
* @author liu_fan 2017年3月30日 上午11:23:10  
*/
public class GenerateFile {
	FreeMarker fm = new FreeMarker("src/main/java/fan/codeGenerate/template");
	//生成文件
	public void generateEntity(List<Map<String, Object>> tableList,GenerateInfo genInfo){
		//创建文件
		this.createFile(genInfo.getEntityFileName(), genInfo.getEntityFilePath());
		this.createFile(genInfo.getServiceIFileName(), genInfo.getServiceIFilePath());
		this.createFile(genInfo.getImplFileName(), genInfo.getImplFilePath());
		this.createFile(genInfo.getDaoFileName(), genInfo.getDaoFilePath());
		//把内容写入文件
		this.writeFile(tableList, genInfo, this.entityFileInfo(tableList, genInfo),genInfo.getEntityFileAllName());
		this.writeFile(tableList, genInfo, this.serviceIFileInfo(tableList, genInfo),genInfo.getServiceIFileAllName());
		this.writeFile(tableList, genInfo, this.implFileInfo(tableList, genInfo),genInfo.getImplFileAllName());
		this.writeFile(tableList, genInfo, this.daoFileInfo(tableList, genInfo),genInfo.getDaoFileAllName());
	}
	//创建文件,返回文件路径
	public void createFile(String classFileName,String classFilePath){
		File filePath = new File(classFilePath);
		if(!filePath.exists()){
			filePath.mkdirs();
		}
		File file = new File(classFilePath,classFileName);
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	//写入文件内容
	public void writeFile(List<Map<String, Object>> tableList,GenerateInfo genInfo,String rs, String readName){
		System.out.println("写文件rs："+rs);
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(readName,true));
			writer.write(rs);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public String entityFileInfo(List<Map<String, Object>> tableList,GenerateInfo genInfo){
    	List<Map<String,Object>> entityList = new ArrayList<Map<String,Object>>();
    	for(Map<String,Object> table : tableList){
    		Map<String,Object> enMap = new HashMap<String,Object>();
    		String dataType = Common.choseType(table.get("DATA_TYPE").toString()); 
    		String lowerName = Common.toFirstLowerName(table.get("COLUMN_NAME").toString());
    		String upperName = Common.toFirstUpperName(table.get("COLUMN_NAME").toString());
    		enMap.put("type", dataType);
    		enMap.put("lowerName", lowerName);
    		enMap.put("upperName", upperName);
    		entityList.add(enMap);
    	}
    	Map<String,Object> root = new HashMap<String,Object>();
    	root.put("entityList", entityList);
    	root.put("entityClassName", genInfo.getEntityClassName());
    	root.put("entityClassPath", genInfo.getEntityClassPath());
    	
    	String rs = fm.getTemplate(root, genInfo.getEntityTemplateName());
    	System.out.println("rs["+rs+"]");
		return rs;
	}
	public String serviceIFileInfo(List<Map<String, Object>> tableList,GenerateInfo genInfo){
		Map<String,Object> root = new HashMap<String,Object>();
    	root.put("serviceIClassName", genInfo.getServiceIClassName());
    	root.put("serviceIClassPath", genInfo.getServiceIClassPath());
    	root.put("entityClassName", genInfo.getEntityClassName());
    	root.put("entityAllName", genInfo.getEntityClassAllName());
    	String rs = fm.getTemplate(root, genInfo.getServiceITemplateName());
    	System.out.println("rs["+rs+"]");
		return rs;
	}
	public String implFileInfo(List<Map<String, Object>> tableList,GenerateInfo genInfo){
		Map<String,Object> root = new HashMap<String,Object>();
    	root.put("implClassName", genInfo.getImplClassName());
    	root.put("implClassPath", genInfo.getImplClassPath());
    	
    	root.put("serviceIClassAllName", genInfo.getServiceIClassAllName());
    	root.put("serviceIClassName", genInfo.getServiceIClassName());
    	root.put("entityClassAllName", genInfo.getEntityClassAllName());
    	root.put("entityClassName", genInfo.getEntityClassName());
    	root.put("daoClassAllName", genInfo.getDaoClassAllName());
    	root.put("daoClassName", genInfo.getDaoClassName());
    	
    	String rs = fm.getTemplate(root, genInfo.getImplTemplateName());
    	System.out.println("rs["+rs+"]");
		return rs;
	}
	public String daoFileInfo(List<Map<String, Object>> tableList,GenerateInfo genInfo){
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		for(Map<String,Object> map : tableList){
			Map<String,Object> m = new HashMap<String,Object>();
    		m.put("type", map.get("DATA_TYPE").toString());
    		m.put("col", Common.toFirstUpperName(map.get("COLUMN_NAME").toString()));
    		mapList.add(m);
		}
		Map<String,Object> root = new HashMap<String,Object>();
		root.put("columns", mapList);
    	root.put("daoClassName", genInfo.getDaoClassName());
    	root.put("daoClassPath", genInfo.getDaoClassPath());
    	
    	root.put("entityClassAllName", genInfo.getEntityClassAllName());
    	root.put("entityClassName", genInfo.getEntityClassName());
    	
    	root.put("tableName", genInfo.getTableName());
    	String rs = fm.getTemplate(root, genInfo.getDaoTemplateName());
    	System.out.println("rs["+rs+"]");
		return rs;
	}
}
